//插件加载
import plugin from '../../../lib/plugins/plugin.js'
import data from '../model/XiuxianData.js'
import fs from "fs"
import { get_ranking_power_img, get_ranking_money_img } from './showData.js'
import {  Read_player,Read_najie } from './xiuxian.js'
import {  existplayer,Get_xiuwei,sortBy,sleep} from './xiuxian.js'
import { __PATH } from "./xiuxian.js"

/**
 * 所有榜单
 */

export class TopList extends plugin {
    constructor() {
        super({
            name: 'Yunzai_Bot_TopList',
            dsc: '修仙模块',
            event: 'message',
            priority: 600,
            rule: [
                {
                    reg: '^#修为榜$',
                    fnc: 'TOP_xiuwei'
                },
                {
                    reg: '^#(财富|肥羊|富豪|灵石)榜$',
                    fnc: 'TOP_lingshi'
                },
            ]
        })
    }

    async TOP_xiuwei(e) {
        let usr_qq = e.user_id;
        let ifexistplay = await existplayer(usr_qq);
        if (!ifexistplay) { return; }
        let usr_paiming;
        let File = fs.readdirSync(__PATH.player_path);
        File = File.filter(file => file.endsWith(".json"));
        let File_length = File.length;
        let temp = [];
        for (var i = 0; i < File_length; i++) {
            let this_qq = File[i].replace(".json", '');
            this_qq = parseInt(this_qq);
            let player = await Read_player(this_qq);
            let sum_exp = await Get_xiuwei(this_qq);
            temp[i] = {
                总修为: sum_exp,
                境界: player.境界,
                名号: player.名号,
                qq: this_qq
            }
        }
        
        //排序
        temp.sort(sortBy("总修为"));
        usr_paiming = temp.findIndex(temp => temp.qq === usr_qq) + 1;
        let Data = [];
        if (File_length > 10) { File_length = 10; }//最多显示前十
        for (var i = 0; i < File_length; i++) {
            temp[i].名次 = i + 1;
            Data[i] = temp[i];
        }
        let thisplayer = await data.getData("player", usr_qq);
        let img = await get_ranking_power_img(e, Data, usr_paiming, thisplayer);
        e.reply(img);
        return;
    }

    

    //TOP_lingshi
    async TOP_lingshi(e) {
        let usr_qq = e.user_id;
        let ifexistplay = await existplayer(usr_qq);
        if (!ifexistplay) { return; }
        let usr_paiming;
        let File = fs.readdirSync(__PATH.player_path);
        File = File.filter(file => file.endsWith(".json"));
        let File_length = File.length;
        let temp = [];
        for (var i = 0; i < File_length; i++) {
            let this_qq = File[i].replace(".json", '');
            this_qq = parseInt(this_qq);
            let player = await Read_player(this_qq);
            let najie = await Read_najie(this_qq);
            let lingshi = player.灵石 + najie.灵石;
            temp[i] = {
                ls1: najie.灵石,
                ls2: player.灵石,
                灵石: lingshi,
                名号: player.名号,
                qq: this_qq
            }
        }
        //排序
        temp.sort(sortBy("灵石"));
        let Data = [];
        usr_paiming = temp.findIndex(temp => temp.qq === usr_qq) + 1;
        if (File_length > 10) { File_length = 10; }//最多显示前十
        for (var i = 0; i < File_length; i++) {
            temp[i].名次 = i + 1;
            Data[i] = temp[i];
        }
        await sleep(500);
        let thisplayer = await data.getData("player", usr_qq);
        let thisnajie = await data.getData("najie", usr_qq);
        let img = await get_ranking_money_img(e, Data, usr_paiming, thisplayer, thisnajie);
        e.reply(img);
        return;

    }



}